package com.example.lending.View;


import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toolbar;

import com.example.lending.Model.Book;
import com.example.lending.Model.User;
import com.example.lending.R;
import com.example.lending.View.Adapter.TypeAdapter;
import com.example.lending.ViewModel.BookViewModel;
import com.example.lending.ViewModel.BorrowViewModel;
import com.example.lending.ViewModel.UserViewModel;
import com.google.android.material.navigation.NavigationView;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

    RecyclerView recyclerView;
    //List<Book> allBooks;
    BookViewModel bookViewModel;
    TypeAdapter typeAdapter;
    BorrowViewModel borrowViewModel;
    UserViewModel userViewModel;
    LiveData<List<Book>> allBooks;
    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        bookViewModel= ViewModelProviders.of(getActivity()).get(BookViewModel.class);
        borrowViewModel=ViewModelProviders.of(getActivity()).get(BorrowViewModel.class);
        userViewModel=ViewModelProviders.of(getActivity()).get(UserViewModel.class);
        allBooks=bookViewModel.getAllBooksLive();
        allBooks.observe(getViewLifecycleOwner(), new Observer<List<Book>>() {
            @Override
            public void onChanged(final List<Book> books) {
                userViewModel.getAllUserLive().observe(getViewLifecycleOwner(), new Observer<List<User>>() {
                    @Override
                    public void onChanged(List<User> users) {
                        typeAdapter=new TypeAdapter(getActivity(),books,borrowViewModel,bookViewModel,userViewModel,users);
                        recyclerView=getView().findViewById(R.id.rv_home);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                        recyclerView.setAdapter(typeAdapter);
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                typeAdapter.notifyDataSetChanged();;
                            }
                        });
                        getActivity().getSharedPreferences("book", Context.MODE_PRIVATE).edit().putBoolean("updata",false).commit();
                    }
                });


            }
        });

//        allBooks=bookViewModel.getAllBooksLive().getValue();
//        typeAdapter=new TypeAdapter(getActivity(),bookViewModel.getAllBooksLive().getValue(),borrowViewModel,bookViewModel,userViewModel);
//        recyclerView=getView().findViewById(R.id.rv_home);
//        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//        recyclerView.setAdapter(typeAdapter);
//        getActivity().runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                typeAdapter.notifyDataSetChanged();;
//            }
//        });
//        getActivity().getSharedPreferences("book", Context.MODE_PRIVATE).edit().putBoolean("updata",false).commit();

    }//左侧弹框功能没有写

    public int getLayoutResId(){return R.layout.fragment_home;}
}
